﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OrderLibrary
{
    [Serializable]
    public class Order : IComparable
    {
        public int Ono { get; set; }//订单号
        public string Gname { get; set; }//商品名
        public string Customer { get; set; }//客户

        public double Oamount { get; set; }//订单金额
        public Order()
        {
            this.Ono = 0;
            this.Gname = string.Empty;
            this.Customer = string.Empty;
            this.Oamount = 0;
        }
        public Order(int onos, string gnames, string customers)
        {
            this.Ono = onos;
            this.Gname = gnames;
            this.Customer = customers;
        }

        private List<OrderDetails> orders = new List<OrderDetails>();
        public List<OrderDetails> Orderss
        {
            get { return orders; }
        }
        public int CompareTo(object o)
        {
            Order item = o as Order;
            return this.Ono.CompareTo(item.Ono);

        }
        public override bool Equals(object o)
        {
            Order item = o as Order;
            return this.Ono == item.Ono;
        }
        public override int GetHashCode()
        {
            return base.GetHashCode();
        }
        public void theWholePrice()
        {
            double sum = 0;
            foreach (OrderDetails n in this.Orderss)
            {
                sum += n.getGAmount();
            }
            this.Oamount = sum;
        }
        public override string ToString()//展示订单明细
        {
            return $"订单号：{Ono}\t商品名：{Gname}\t客户名：{Customer}\t总金额：{Oamount}";
        }
        public void AddDetails(int id, string name, int num, double price)
        {
            bool isEqual = false;
            OrderDetails i = new OrderDetails(id, name, num, price);
            foreach (OrderDetails o in this.Orderss)
            {
                if (i.Equals(o))
                {
                    isEqual = true;
                    Console.WriteLine("订单内编号重复！");
                }
            }
            if (!isEqual)
            {
                this.Orderss.Add(i);
                Console.WriteLine("添加成功");
            }
        }
        public void ChangeDetails(int sele3_1, int Eoid, string gnamed, int gnumd, double price)
        {
            try
            {
                int m, n;
                m = Eoid;
                n = sele3_1;
                switch (n)
                {
                    case 1:
                        string gnamed1 = gnamed;
                        this.Orderss.ForEach(i =>
                        {
                            if (i.OId == m)
                            {
                                i.Goodname = gnamed1;
                            }
                        }
                        );
                        break;
                    case 2:
                        int gnumd1 = gnumd;
                        this.Orderss.ForEach(i =>
                        {
                            if (i.OId == m)
                            {
                                i.GoodNum = gnumd1;
                            }
                        }
                        );
                        break;
                    case 3:
                        double price1 = price;
                        this.Orderss.ForEach(i =>
                        {
                            if (i.OId == m)
                            {
                                i.UnitPrice = price1;
                            }
                        }
                        );
                        break;
                }
            }
            catch
            {
                Console.WriteLine("Error!请检查是否输入错误的单内编号/修改选项/不符修改项类型的数据");
            }
        }
        public void DeleteDatails(int OidTODel)
        {
            try
            {
                int m = OidTODel;
                for (int i = this.Orderss.Count - 1; i >= 0; i--)
                {
                    if (this.Orderss[i].OId == m)
                    {
                        this.Orderss.Remove(this.Orderss[i]);
                    }
                }
                Console.WriteLine("已删除");
            }
            catch
            {
                Console.WriteLine("Error!是否输入了不存在/错误的单内编号？");
            }
        }
        public void SortDetails()
        {
            Console.WriteLine("输入1/2/3分别按默认单内编号排序/商品数量/商品单价排序");
            int selec1 = Convert.ToInt32(Console.ReadLine());
            switch (selec1)
            {
                case 1:
                    this.Orderss.Sort();
                    break;
                case 2:
                    List<OrderDetails> sortDetails = this.Orderss.OrderBy(s => s.GoodNum).ToList<OrderDetails>();
                    this.orders = sortDetails;
                    break;
                case 3:
                    List<OrderDetails> sortDetails2 = this.orders.OrderBy(s => s.UnitPrice).ToList<OrderDetails>();
                    this.orders = sortDetails2;
                    break;
                default:
                    Console.WriteLine("Error!是否输入错误？");
                    break;
            }
        }

    }

}

